package com.niuzi.servlet;

import com.niuzi.entity.Admin;
import com.niuzi.util.DataUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author Lee
 * 2022/4/10 星期日 22:59
 */
@WebServlet(urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String againpassword = req.getParameter("againpassword");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        String jiguan = req.getParameter("jiguan");
        String[] powers = req.getParameterValues("power");

        System.out.println(username +" "+password+" "+againpassword+" "+name+" "+birthday+" "+
                birthday+" "+ Arrays.toString(powers)+" "+jiguan+" "+sex);

        if (password == null || password.equals("") || !password.equals(againpassword)){
            resp.sendRedirect("register.jsp?status=1");
            return;
        }

        if (username == null || username.equals("")){
            resp.sendRedirect("register.jsp?status=2");
            return;
        }

        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        admin.setName(name);
        admin.setBirthday(birthday);
        admin.setPowers(powers);
        admin.setJiguan(jiguan);
        admin.setSex(sex);

        DataUtil.addAdmin(admin);

        resp.sendRedirect("login.jsp");

    }
}
